/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;
import net.worlds.ui.edge.EComponent;
import org.jbox2d.common.Vec2D;


public abstract class UIComponent extends GameObject{
    protected EComponent component;
    protected boolean componentAdded = false;
    public UIComponent(String type) throws WorldsException
    {
        super(type);
        addPropChangeHandler("Size", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                Vec2D vec = (Vec2D) value;
                component.setWidth((int) vec.x);
                component.setHeight((int) vec.y);
            }
        });
        addPropChangeHandler("Layer", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(componentAdded)
                    component.setLayer((Integer) value);
            }
        });
        addPropChangeHandler("Position", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                Vec2D vec = (Vec2D) value;
                component.setX((int) vec.x);
                component.setY((int) vec.y);
            }
        });
        addPropChangeHandler("Tooltip", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                if(value.equals(""))
                    component.setTooltip(null);
                else
                    component.setTooltip(value.toString());
            }
        });
        addPropChangeHandler("Parent", new PropChangeHandler()
        {

            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                onParentChanged((GameObject) oldValue, (GameObject) value);
                if(value instanceof UIComponent && !componentAdded)
                {
                    UIComponent c = (UIComponent) value;
                    if(c.componentAdded)
                    {
                        c.component.addChild(getIntProp("Layer"), component);
                        componentAdded = true;
                        add();
                    }
                }
                else if(value != null && componentAdded && !(value instanceof UIComponent) && !(instance instanceof PlayerUI))
                {
                    destroy();
                }
            }
        });
    }
    @Override
    public void addToWorld() throws WorldsException
    {
        try {
            if(getParent() instanceof UIComponent && !componentAdded)
            {
                UIComponent c = (UIComponent) getParent();
                if (c.componentAdded) {
                    c.component.addChild(getIntProp("Layer"), component);
                    componentAdded = true;
                    add();
                }
            }
        } catch (WorldsException ex) {
            throw new RuntimeException(ex);
        }
        super.addToWorld();
    }
    @Override
    public void destructor() throws WorldsException
    {
        super.destructor();
        destroy();
    }
    public void destroy()
    {
        if(componentAdded)
        {
            component.remove();
            componentAdded = false;
        }
    }
    @Override
    public Object getProp(String name) throws WorldsException
    {
        if(name.equals("Position"))
            return new Vec2D(component.x, component.y);
        else if(name.equals("PositionOnScreen"))
            return new Vec2D(component.getX(), component.getY());
        else if(name.equals("Size"))
            return new Vec2D(component.getWidth(), component.getHeight());
        else
            return super.getProp(name);
    }
    public void add()
    {
        
    }
}
